package ${package.Task}.once;

import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import ${package.Dao}.${entity}EsDao;
import ${package.Entity}.${entity};
import ${package.Entity}.${entity}Es;
import ${package.Service}.${table.serviceName};
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * ${table.comment!} => 全量同步到 Elasticsearch
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Slf4j
// todo 取消注释开启任务
// @Component
public class FullSync${entity}ToEs implements CommandLineRunner {

    @Resource
    private ${table.serviceName} ${table.layuiServiceName};

    @Resource
    private ${entity}EsDao ${table.layuiEntityName}EsDao;

    @Override
    public void run(String... args) {
        List<${entity}> list = ${table.layuiServiceName}.list();
        if (list.isEmpty()) return;
<#assign entityEs = entity + 'Es' />
        List<${entityEs}> esList = list.stream().map(${entity} :: transform).collect(Collectors.toList());
        final int pageSize = 500;
        int total = esList.size();
        log.info("FullSync${entity}ToEs start, total {}", total);
        for (int i = 0; i < total; i += pageSize) {
            int end = Math.min(i + pageSize, total);
            log.info("sync from {} to {}", i, end);
            ${table.layuiEntityName}EsDao.saveAll(esList.subList(i, end));
        }
        log.info("FullSync${entity}ToEs end, total {}", total);
    }

}
